home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / TupleDatabase / TupleTests.cp < prev    next >
Encoding:
Text File  |  1995-07-28  |  10.4 KB  |  373 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        TupleTests.cp
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13. #if 0
  14. #ifndef    __TUPLETESTS__
  15. #include "TupleTests.h"
  16. #endif
  17.  
  18. #ifndef __BTREETUPLEDATABASE__
  19. #include "BTreeTupleDatabase.h"
  20. #endif
  21.  
  22. #ifndef __HFSTUPLEDATABASE__
  23. #include "HFSTupleDatabase.h"
  24. #endif
  25.  
  26. #ifndef __RAMTUPLEDATABASE__
  27. #include "RamTupleDatabase.h"
  28. #endif
  29.  
  30. #ifndef    __STRING__
  31. #include "String.h"
  32. #endif
  33.  
  34. #ifndef    __STDLIB__
  35. #include "StdLib.h"
  36. #endif
  37.  
  38. #ifndef    __MEMORY__
  39. #include "Memory.h"
  40. #endif
  41.  
  42. #pragma segment TupleTests
  43.  
  44. /***********************************|****************************************/
  45.  
  46. void FolderTest ()
  47. {
  48.     TEST_REPORT(FolderTest);
  49.     TFolderSpec folder ( "folder.test" );
  50.     folder >> chris;
  51.     
  52.     TFolderFileIterator iterator ( folder );
  53.     const TFileSpec* fileSpec;
  54.     unsigned long i = 0;
  55.     
  56.     for ( fileSpec = iterator.FirstFile ();
  57.           fileSpec != nil;
  58.           fileSpec = iterator.NextFile () )
  59.     {
  60.         chris << "\nFile " << ++i << "\n";
  61.         *fileSpec >> chris;
  62.     }    
  63. }
  64.  
  65. /***********************************|****************************************/
  66.  
  67. Boolean
  68. InsertTest ( ATupleDatabase& database, const ATupleKey& key, const ADataItem& data )
  69. {
  70. // save precondition state
  71.     Boolean alreadyExists = database.DoesTupleExist ( key );
  72.     unsigned long preCount = database.CountTuples ();
  73.  
  74.     ASSERT_RETURN_ZERO ( database.SetTuple ( key, data ) );
  75.     ASSERT_RETURN_ZERO ( database.DoesTupleExist ( key ) );
  76.     unsigned long postCount = database.CountTuples ();
  77.     ASSERT_RETURN_ZERO ( postCount == ( alreadyExists ? preCount : preCount + 1 ) );
  78.  
  79. // make sure we can do a direct retrieve
  80.     CDataItem tempData;
  81.     ASSERT_RETURN_ZERO ( database.GetTupleData ( key, tempData ) );
  82.     ASSERT_RETURN_ZERO ( tempData == data );
  83.     unsigned long size = 0;
  84.     ASSERT_RETURN_ZERO ( database.GetTupleDataSize ( key, size ) );
  85.     ASSERT_RETURN_ZERO ( size == data.GetPhysicalLength () );
  86.     ASSERT_RETURN_ZERO ( size == tempData.GetPhysicalLength () );
  87.  
  88. // make sure we can do an indirect retrieve
  89.     unsigned long index = 0;
  90.     ASSERT_RETURN_ZERO ( database.FindIndexOfTuple ( key, index ) );
  91.     CTupleKey tempKey; tempData.SetPhysicalLength ( 0 );
  92.     ASSERT_RETURN_ZERO ( database.GetTuple ( index, tempKey, tempData ) );
  93.     ASSERT_RETURN_ZERO ( tempKey == key );
  94.     ASSERT_RETURN_ZERO ( tempData == data );
  95.     tempKey.SetLength ( 0 );
  96.     ASSERT_RETURN_ZERO ( database.GetTupleKey ( index, tempKey ) );
  97.     ASSERT_RETURN_ZERO ( tempKey == key );
  98.     tempData.SetPhysicalLength ( 0 );
  99.     ASSERT_RETURN_ZERO ( database.GetTupleData ( index, tempData ) );
  100.     ASSERT_RETURN_ZERO ( tempData == data );
  101.     
  102.     return true;
  103. }
  104.  
  105. /***********************************|****************************************/
  106. /***********************************|****************************************/
  107.  
  108. class CWriteBuffer : public CDataItem
  109. {
  110. public:    CWriteBuffer ( const char* );
  111. };
  112.  
  113. CWriteBuffer::CWriteBuffer ( const char* s ):
  114.     CDataItem ( s, strlen ( s ) )
  115. {
  116. }
  117.  
  118. /***********************************|****************************************/
  119.  
  120. void
  121. TupleDatabaseTest ( ATupleDatabase& database, const ATupleKey& key1, const ATupleKey& key2, const ATupleKey& key3 )
  122. {
  123.     TEST_REPORT(TupleDatabaseTest);
  124.  
  125.     const CWriteBuffer wrData1 ( "data 1 string which is rather long" );
  126.     const CWriteBuffer wrData2 ( "data 2 string  which is even longer than the first" );
  127.     const CWriteBuffer wrData3 ( "data 3 string is shorter" );
  128.     const CWriteBuffer wrData4 ( "new data for key 1" );
  129.     const CWriteBuffer wrData5 ( "key 2 new data" );
  130.     const CWriteBuffer wrData6 ( "data for key 3" );
  131.  
  132.     ASSERT_RETURN ( InsertTest ( database, key1, wrData1 ) );
  133.     ASSERT_RETURN ( InsertTest ( database, key2, wrData2 ) );
  134.     ASSERT_RETURN ( InsertTest ( database, key1, wrData4 ) );
  135.     ASSERT_RETURN ( InsertTest ( database, key3, wrData6 ) );
  136.     ASSERT_RETURN ( InsertTest ( database, key1, wrData1 ) );
  137.     ASSERT_RETURN ( InsertTest ( database, key3, wrData3 ) );
  138.     ASSERT_RETURN ( InsertTest ( database, key2, wrData5 ) );
  139.     ASSERT_RETURN ( InsertTest ( database, key1, wrData4 ) );
  140. }
  141.  
  142. /***********************************|****************************************/
  143.  
  144. void HFSDatabaseTest ()
  145. {
  146.     TEST_REPORT(HFSDatabaseTest);
  147.     TFolderSpec folder ( "hfs.test" );
  148.     folder.DeleteFolder ();
  149.     THFSTupleDatabase database ( folder );
  150.     const CTupleKey key1 ( "\pkey 1 (one)" ), key2 ( "\pkey 2 (two, two keys in one)" ), key3 ( "\pkey 3 (three, or thrice)" );
  151.     TupleDatabaseTest ( database, key1, key2, key3 );    // run tests with new folder
  152.     TupleDatabaseTest ( database, key1, key2, key3 );    // run tests with existing folder
  153. }
  154.  
  155. /***********************************|****************************************/
  156.  
  157. class CLongShortShortDescriptor : public AKeyDescriptor
  158. {
  159. public:                                CLongShortShortDescriptor ();
  160.     virtual                            ~CLongShortShortDescriptor ();
  161.         
  162.     virtual    const unsigned char*    GetBtreeDescriptor () const;
  163. };
  164.  
  165. /***********************************|****************************************/
  166.  
  167. class CLongShortShortKey : public ATupleKey
  168. {
  169. public:                                CLongShortShortKey ( long, short, short );
  170.     virtual                            ~CLongShortShortKey ();
  171.  
  172.     virtual    const void*                GetData () const;
  173.     virtual    unsigned long            GetLength () const;
  174.     virtual    unsigned long            SetLength ( unsigned long );
  175.  
  176.     virtual    long                    Compare ( const AComparable& tupleKeysOnly ) const;
  177.     virtual    ostream&                operator >> ( ostream& ) const;
  178.  
  179. private:    
  180.  
  181.     struct Data { long fLong; short fShort1, fShort2; };
  182.     
  183.             Data                    fData;
  184. };
  185.  
  186. /***********************************|****************************************/
  187. /***********************************|****************************************/
  188.  
  189. CLongShortShortDescriptor::CLongShortShortDescriptor ():
  190.     AKeyDescriptor ()
  191. {
  192. }
  193.  
  194. /***********************************|****************************************/
  195.  
  196. CLongShortShortDescriptor::~CLongShortShortDescriptor ()
  197. {
  198. }
  199.  
  200. /***********************************|****************************************/
  201.  
  202. const unsigned char*
  203. CLongShortShortDescriptor::GetBtreeDescriptor () const
  204. {
  205.     static unsigned char kDescriptor [] = { 4, kdLong, 1, kdWord, 2 };
  206.     return kDescriptor;
  207. }
  208.  
  209. /***********************************|****************************************/
  210. /***********************************|****************************************/
  211.  
  212. CLongShortShortKey::CLongShortShortKey ( long l, short s1, short s2 ):
  213.     ATupleKey ()
  214. {
  215.     fData.fLong = l;
  216.     fData.fShort1 = s1;
  217.     fData.fShort2 = s2;
  218. }
  219.  
  220. /***********************************|****************************************/
  221.  
  222. CLongShortShortKey::~CLongShortShortKey ()
  223. {
  224. }
  225.  
  226. /***********************************|****************************************/
  227.  
  228. const void*
  229. CLongShortShortKey::GetData () const
  230. {
  231.     return &fData;
  232. }
  233.  
  234. /***********************************|****************************************/
  235.  
  236. unsigned long
  237. CLongShortShortKey::GetLength () const
  238. {
  239.     return sizeof ( fData );
  240. }
  241.  
  242. /***********************************|****************************************/
  243.  
  244. unsigned long
  245. CLongShortShortKey::SetLength ( unsigned long length )
  246. {
  247.     return sizeof ( fData );
  248. }
  249.  
  250. /***********************************|****************************************/
  251.  
  252. long 
  253. CLongShortShortKey::Compare ( const AComparable& other ) const
  254. {
  255.     const CLongShortShortKey& that = (const CLongShortShortKey&) other;
  256.     ASSERT ( GetLength () == that.GetLength () );
  257.     const Data& thatData = *(const Data*) that.GetData ();
  258.  
  259.     return
  260.         fData.fLong == thatData.fLong &&
  261.         fData.fShort1 == thatData.fShort1 &&
  262.         fData.fShort2 == thatData.fShort2;
  263. }
  264.  
  265. /***********************************|****************************************/
  266.  
  267. ostream&
  268. CLongShortShortKey::operator >> ( ostream& stream ) const
  269. {
  270.     stream << "CLongShortShortKey @ " << (void*) this << '\n';
  271.     ATupleKey::operator >> ( stream );
  272.     stream << "\tfData.fLong: " << fData.fLong << endl;
  273.     stream << "\tfData.fShort1: " << fData.fShort1 << endl;
  274.     return stream << "\tfData.fShort2: " << fData.fShort2 << endl;
  275. }
  276.  
  277. /***********************************|****************************************/
  278.  
  279. void BTreeDatabaseTest ()
  280. {
  281.     TEST_REPORT(BTreeDatabaseTest);
  282.  
  283. #if 0
  284.  
  285.     const CLongShortShortDescriptor kDescriptor;
  286.     const CLongShortShortKey key1 ( 1, 2, 3 );
  287.     const CLongShortShortKey key2 ( 4, 5, 6 );
  288.     const CLongShortShortKey key3 ( 7, 8, 9 );
  289.  
  290. #else
  291.  
  292.     const AKeyDescriptor& kDescriptor = AKeyDescriptor::kString;
  293.     const CTupleKey key1 ( "\pkey 1 (one)", true );
  294.     const CTupleKey key2 ( "\pkey 2 (two, two keys in one)", true );
  295.     const CTupleKey key3 ( "\pkey 3 (three, or thrice)", true );
  296.  
  297. #endif
  298.  
  299.     TFileSpec file ( "btree.test" );
  300.     file.DeleteFile ();
  301.     chris << kDescriptor << endl;
  302.     TBTreeDatabase database ( file, kDescriptor, TBTreeDatabase::kReadWriteNew, TBTreeDatabase::kNonExclusive );
  303.     TupleDatabaseTest ( database, key1, key2, key3 );    // once with new file
  304.     TupleDatabaseTest ( database, key1, key2, key3 );    // once with old file
  305.  
  306.     TFileSpec file2 ( "btree2.test" );
  307.     file2.DeleteFile ();
  308.     chris << kDescriptor << endl;
  309.     TBTreeDatabase database2 ( file2, kDescriptor, TBTreeDatabase::kReadWriteNew, TBTreeDatabase::kExclusive );
  310.     TupleDatabaseTest ( database2, key1, key2, key3 );    // once with new file
  311.     TupleDatabaseTest ( database2, key1, key2, key3 );    // once with old file
  312. }
  313.  
  314. /***********************************|****************************************/
  315.  
  316. void RamDatabaseTest ()
  317. {
  318.     TRamTupleDatabase database;
  319.     const CFourByteKey key1 ( (unsigned long) 235656 ), key2 ((long) -45344 ), key3 ((void*) 0x482efd);
  320.     TupleDatabaseTest ( database, key1, key2, key3 );
  321. }
  322.  
  323. /***********************************|****************************************/
  324.  
  325. void AllDatabaseTests ()
  326. {
  327.     HFSDatabaseTest ();
  328.     RamDatabaseTest ();
  329.     BTreeDatabaseTest ();
  330. }
  331.  
  332. /***********************************|****************************************/
  333.  
  334. void TupleTests ()
  335. {
  336.     TEST_REPORT(TupleTests);
  337.     CTupleKey key1 ( "key 1" ), key2 ( "key2" );
  338.     ASSERT ( key1 != key2 );
  339.     key1 = key2;
  340.     ASSERT ( key1 == key2 );
  341. }
  342.  
  343. /***********************************|****************************************/
  344.  
  345. #define    PRINT_SIZE(C) chris << "sizeof ( " << #C << ") = " << sizeof ( C ) << '\n'
  346.  
  347. void
  348. PrintSizes ()
  349. {
  350.     PRINT_SIZE ( TFileSpec );
  351.     PRINT_SIZE ( TFolderSpec );
  352.  
  353.     PRINT_SIZE ( ADataItem );
  354.     PRINT_SIZE ( CDataItem );
  355.     PRINT_SIZE ( CPrefixDataItem );
  356.     PRINT_SIZE ( CWrapperBuffer );
  357.  
  358.     PRINT_SIZE ( ATupleKey );
  359.     PRINT_SIZE ( CTupleKey );
  360.     PRINT_SIZE ( CFourByteKey );
  361.  
  362.     PRINT_SIZE ( AKeyDescriptor );
  363.     PRINT_SIZE ( CWrapperBuffer );
  364.  
  365.     PRINT_SIZE ( ATupleDatabase );
  366.     PRINT_SIZE ( TRamTupleDatabase );
  367.     PRINT_SIZE ( THFSTupleDatabase );
  368.     PRINT_SIZE ( TBTreeDatabase );
  369. }
  370.  
  371. /***********************************|****************************************/
  372. #endif
  373.